package com.yazi.mxz.common.db.dao.impl;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import com.myframework.smhj.hibernate.dao.BasicDao;

import com.yazi.mxz.common.db.dao.CookDao;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.entity.beans.Cook;

@Repository("cookDao")
public class CookDaoImpl extends BasicDao<Cook> implements CookDao {

	
	public CookDaoImpl() {
		super(Cook.class);
		
	}

	@Override
	public Criteria getExample(Cook cook, String searchWord) {
		String keywords = null;
		if (null != cook) {
			keywords = cook.getKeywords();
			cook.setKeywords(null);
		}
		
		Criteria c = super.getExample(cook, searchWord);
		
		// 关联对象条件
		if (null != cook) {
			// 菜品分类
			Category categoryType = cook.getCategoryByTypeId();
			if (null != categoryType) {
				// 分类ID
				if (null != categoryType.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + categoryType.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("categoryByTypeId.catId", dc));
				} else {
					// ...分类的其他条件					
				}
			}
			// 菜系分类
			Category categoryStyle = cook.getCategoryByStyleId();
			if (null != categoryStyle) {
				// 分类ID
				if (null != categoryStyle.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + categoryStyle.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("categoryByStyleId.catId", dc));
				} else {
					// ...分类的其它条件					
				}
				
			}
			// 用餐时间分类
			Category categoryTime = cook.getCategoryByTimeId();
			if (null != categoryTime) {
				// 分类ID
				if (null != categoryTime.getCatId()) {
					DetachedCriteria dc = DetachedCriteria.forClass(Category.class);
					dc.add(Restrictions.sqlRestriction("1=1 START WITH cat_id=" + categoryTime.getCatId() + " CONNECT BY PRIOR cat_id= parent_id "));
					dc.setProjection(Projections.property("catId"));
					c.add(Subqueries.propertyIn("categoryByTimeId.catId", dc));
				} else {
					// ...分类的其它条件					
				}				
			}
			// ...其它关联对象条件
		}
		
		// 关键字条件1
		if(null != keywords){
			String[] keywordList = null;
			keywordList = keywords.split(",");
			
			List<LogicalExpression> leList = new ArrayList<LogicalExpression>();
			
			//遍历待查询的关键字，将其加入条件中
			for(String keyword : keywordList){
				if(null != keyword && !keyword.isEmpty()){
					String likeWord0 = keyword;
					String likeWord1 = keyword + ",%";
					String likeWord2 = "%," + keyword + ",%";
					String likeWord3 = "%," + keyword;
					LogicalExpression el = Restrictions.or(Restrictions.like("keywords", likeWord0),
							 	Restrictions.or(Restrictions.like("keywords", likeWord1),
							 			Restrictions.or(Restrictions.like("keywords", likeWord2),
							 					Restrictions.like("keywords", likeWord3))));
					leList.add(el);
				}
				
			}			
			LogicalExpression[] les = new LogicalExpression[leList.size()];
			for (int i = 0; i < leList.size(); i++) {
				les[i] = leList.get(i);
			}
			c.add(Restrictions.or(les));
		}
		
		// 关键字条件2
		if (null != searchWord && !searchWord.isEmpty()) {
			//关键字查询
			String likeWord = "%" + searchWord + "%";
			c.add(Restrictions.or(Restrictions.like("name", likeWord),
					Restrictions.or(Restrictions.like("keywords", likeWord))));
		}

		return c;
	}

}
